var id = getQueryString('id');
var flag = true;
$(function(){
    $('#childSiteId').blur(function() {
        $.ajax({
            url: webConfig.webUrl + '/sseSubStation/checkSubStationNo',
            type: 'POST',
            dataType: 'json',
            data: {subStationNo:$(this).val()},
            success: function(data) {
                if (data.code == webConfig.code.SUCCESS) {
                    checkInput("childSiteId","error");
                    flag = true;
                } else {
                    checkInput("childSiteId","error",1,data.message);
                    flag = false;
                }
            }
        });
    });
});

if (id) {
    $.ajax({
        url: webConfig.webUrl + '/sseSubStation/getSubStationById',
        type: 'post',
        dataType: 'json',
        data: {
            ids: id
        },
        success: function(data) {
            var result = data.result;
            document.getElementById('childSiteId').value = result[0].subStationNo;
            $("#childSiteId").attr('disabled', true);
            document.getElementById('childSiteName').value = result[0].subStationName;
        }
    });
}

$('#btnSubmit').click(function() {
    if(!flag){
        layer.alert('站点编号不能重复！',{icon:2});
        return false;
    }
    var obj = {
            subStationId: id,
            subStationNo: $.trim(document.getElementById('childSiteId').value),
            subStationName: document.getElementById('childSiteName').value,
        };
    var reg = /^(?!_+|\d+)[\w\u4e00-\u9fa5]{2,20}$/;

    if (!obj.subStationNo) {
        parent.layer.alert('站点编号不能为空', {
            icon: 2
        });
        return false;
    }
    if (!validator(obj.subStationNo, "number")) {
        parent.layer.alert('站点编号为数值', {
            icon: 2
        });
        return false;
    }
    if (obj.subStationNo == 0) {
        parent.layer.alert('站点编号不能为0', {
            icon: 2
        });
        return false;
    }
    if (!obj.subStationName) {
        parent.layer.alert('站点名称不能为空', {
            icon: 2
        });
        return false;
    }
    if (!reg.test(obj.subStationName)) {
        parent.layer.alert('请输入以汉字或字母开头，2~20位（汉字、字母、数字、下划线）组成的站点名称', {
            icon: 2
        });
        return false;
    }

    addConfirm(obj);

});


/*提交*/
function addConfirm(obj) {
    var url = webConfig.webUrl + (id ? '/sseSubStation/updateSubStation' : '/sseSubStation/addSubStation');
    parent.layer.confirm('是否确认提交？', {
        title: '提示',
        icon: 3
    }, function() {
        $.ajax({
            url: url,
            type: 'post',
            dataType: 'json',
            data: obj,
            success: function(data) {
                checkResultCode({
                    code: data.code,
                    msg: data.message
                }, (id ? '编辑' : '添加') + '站点成功', 1, 1);
            }
        });
    });


}

//点击取消
$('#goback').click(function() {
    parent.layer.closeAll('iframe');
});